package com.controller;



import com.entity.ChessGame;
import com.entity.GamePool;
import com.server.WebSocketServer;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/chess")
public class ChessGameController {

    @RequestMapping("/color")
    public List<Map<String, Integer>> color(String room){
        ChessGame chessGame = (ChessGame)GamePool.gmaePoll.get(room);
        return chessGame.selectColor();
    }

    @RequestMapping("/paint")
    public void paint(String room, Integer id) throws IOException {
        ChessGame chessGame = (ChessGame)GamePool.gmaePoll.get(room);
        chessGame.paint(id, 1);
        WebSocketServer.sendInfo(room, "");
    }

    @RequestMapping("/creatroom")
    public Integer creatroom(String room) {
        Integer ret = -1;
        if(!GamePool.gmaePoll.containsKey(room)) {
            ChessGame chessGame = new ChessGame();
            GamePool.gmaePoll.put(room, chessGame);
            chessGame.login(0, "0");
            ret = 0;
        }
        return ret;
    }

    @RequestMapping("/join")
    public Integer join(String room){

        Integer ret = -1;
        if(GamePool.gmaePoll.containsKey(room)){
            ChessGame chessGame = (ChessGame)GamePool.gmaePoll.get(room);
            Map<String, String[]> roomUser = WebSocketServer.getRoomUser();
            String[] users = roomUser.get(room);
            if(users[0] == null){
                ret = 0;
            } else {
                ret = 1;
            }
            chessGame.login(ret,"0");
        }
        return ret;
    }

    @RequestMapping("/reset")
    public void reset(){
//        chessMapper.giveUpAll();
    }

    @RequestMapping("restart")
    public void restart(String room) throws IOException {
        ChessGame chessGame = (ChessGame) GamePool.gmaePoll.get(room);

        chessGame.setDbCount(0, 10);
        chessGame.setDbCount(1, 10);

        chessGame.aClear();
        chessGame.bClear();


        chessGame.paint(76, 1);
        chessGame.paint(4, 2);

        chessGame.chessSet(0, 76);
        chessGame.chessSet(1, 4);
        WebSocketServer.sendInfo(room, "restart");
    }

    @RequestMapping("/move")

    public boolean move(Integer type, String room, Integer mychess) throws IOException {
        ChessGame chessGame = (ChessGame) GamePool.gmaePoll.get(room);
        Integer place = chessGame.getPlace(mychess);
        Integer oth_id = (mychess == 1 ? 0 : 1);
        Integer oth_place = chessGame.getPlace(oth_id);
        boolean ret = true;
        int newplace = -1;
        switch (type){
            case 1:
                if(place - 9 >= 0) {
                    //向上走place:0, 和place-1:0
                    if(
                        chessGame.empty(place, 0) == 0 &&
                        chessGame.empty(place-1, 0) == 0
                    ) {
                        newplace = place - 9;

                        if (newplace == oth_place && newplace - 9 >= 0) {
                            if(
                                chessGame.empty(newplace, 0) == 0 &&
                                chessGame.empty(newplace-1, 0) == 0
                            ) {
                                newplace -= 9;
                            }
                        }
                    }
                }
                break;
            case 2:
                if(place + 9 < 81) {
                    //向下走place+9:0, 和place+8:0
                    if(
                        chessGame.empty(place+9, 0) == 0 &&
                        chessGame.empty(place+8, 0) == 0
                    ) {
                        newplace = place + 9;

                        if (newplace == oth_place && newplace + 9 < 81) {
                            if(
                                chessGame.empty(newplace+9, 0) == 0 &&
                                chessGame.empty(newplace+8, 0) == 0
                            ) {
                                newplace += 9;
                            }
                        }
                    }
                }
                break;
            case 3:
                if(place % 9 != 0) {
                    //向左走place:90, 和place-9:90
                    if(chessGame.empty(place, 90) == 0) {
                        if(place - 9 >= 0){
                            if(chessGame.empty(place-9, 90) != 0){
                                break;
                            }
                        }
                        newplace = place - 1;
                        if (newplace == oth_place && newplace % 9 != 0) {
                            if(chessGame.empty(newplace, 90) == 0) {
                                if (place - 9 >= 0) {
                                    if (chessGame.empty(newplace - 9, 90) != 0) {
                                        break;
                                    }
                                }
                                newplace--;
                            }
                        }

                    }
                }
                break;
            case 4:
                if((place + 1) % 9 != 0) {
                    //向右走place+1:90, 和place-8:90
                    if(chessGame.empty(place+1, 90) == 0) {
                        if (place - 8 >= 0) {
                            if (chessGame.empty(place - 8, 90) != 0) {
                                break;
                            }
                        }
                        newplace = place + 1;
                        if (newplace == oth_place && (newplace + 1) % 9 != 0) {
                            if(chessGame.empty(newplace+1, 90) == 0) {
                                if (place - 8 >= 0) {
                                    if (chessGame.empty(newplace - 8, 90) != 0) {
                                        break;
                                    }
                                }
                                newplace++;
                            }
                        }
                    }
                }
        }

        if (newplace != -1 && newplace != oth_place) {
            chessGame.setNowmove(oth_id);
            chessGame.paint(place, 0);
            chessGame.paint(newplace, mychess + 1);
            chessGame.chessSet(mychess, newplace);
            WebSocketServer.sendInfo(room, mychess.toString());
        } else {
            ret = false;
        }
        return  ret;
    }

    @RequestMapping("/dbplace")

    public boolean dbplace(Integer id, Integer rotate, String room, Integer mychess) throws IOException {
        ChessGame chessGame = (ChessGame) GamePool.gmaePoll.get(room);
        Integer other_chess = mychess == 0 ? 1 : 0;
        if(rotate == 0){
            if((id + 1) % 9 == 0){
                return false;
            }
            //id - 1和id + 1的 0度有没有id - 8 90有没有
            if(id % 9 != 0){
                if(chessGame.empty(id - 1, 0) != 0) {
                    return false;
                }
            }
            if((id + 1) % 9 != 0) {
                if(chessGame.empty(id + 1, 0) != 0) {
                    return false;
                }
            }
            if((id + 1) % 9 != 0 && id - 9 >= 0) {
                if(chessGame.empty(id - 8, 90) != 0) {
                    return false;
                }
            }

        } else {
            if(id >= 72){
                return false;
            }
            //id - 9和id + 9的 90度有没有id + 8 0度有没有
            if(id - 9 >= 0) {
                if(chessGame.empty(id - 9, 90) != 0) {
                    return false;
                }
            }
            if(id + 9 < 81) {
                if(chessGame.empty(id + 9, 90) != 0) {
                    return false;
                }
            }
            if(id % 9 != 0 && id + 9 < 81) {
                if(chessGame.empty(id + 8, 0) != 0) {
                    return false;
                }
            }
        }
        chessGame.setNowmove(mychess == 0 ? 1 : 0);
        if(getWin(room, mychess, id, rotate) && getWin(room, other_chess, id, rotate)){
            chessGame.dbSet(id, rotate);
        } else {
            return false;
        }

        chessGame.setDbCount(mychess, chessGame.getDbCount(mychess) - 1);
        WebSocketServer.sendInfo(room,mychess.toString());
        return true;
    }

    @RequestMapping("/getdb")
    public List<Map<String, Integer>> getdb(String room){
        ChessGame chessGame = (ChessGame) GamePool.gmaePoll.get(room);
        return chessGame.getDb();
    }

    @RequestMapping("/getnowmove")
    public Integer getnowmove(String room){
        ChessGame chessGame = (ChessGame) GamePool.gmaePoll.get(room);
        return chessGame.getNowmove();
    }

    @RequestMapping("/getdbcount")
    public Integer getdbcount(String room, Integer index){
        ChessGame chessGame = (ChessGame) GamePool.gmaePoll.get(room);
        return chessGame.getDbCount(index);
    }

    public boolean getWin(String room, Integer chess, Integer newdb_index, Integer newdb_rotate) throws IOException {
        List<Integer> ret = new ArrayList<>();
        ChessGame chessGame = (ChessGame) GamePool.gmaePoll.get(room);
        List<Map<String, Integer>> db = chessGame.getDb();

        List<Integer>[] arr = new ArrayList[81];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new ArrayList<>();
        }
        if (newdb_rotate == 0) {
            if (newdb_index >= 9) {
                arr[newdb_index].add(newdb_index - 9);
                arr[newdb_index + 1].add(newdb_index - 8);

                arr[newdb_index - 9].add(newdb_index);
                arr[newdb_index - 8].add(newdb_index + 1);
            }
        } else {
            if (newdb_index % 9 != 0) {
                arr[newdb_index].add(newdb_index - 1);
                arr[newdb_index + 9].add(newdb_index + 8);

                arr[newdb_index - 1].add(newdb_index);
                arr[newdb_index + 8].add(newdb_index + 9);
            }
        }
        if(db != null) {
            for (Map<String, Integer> i : db) {
                Integer id = i.get("id");
                Integer rotate = i.get("rotate");
                if (rotate == 0) {
                    if (id >= 9) {
                        arr[id].add(id - 9);
                        arr[id + 1].add(id - 8);

                        arr[id - 9].add(id);
                        arr[id - 8].add(id + 1);
                    }
                } else {
                    if (id % 9 != 0) {
                        arr[id].add(id - 1);
                        arr[id + 9].add(id + 8);

                        arr[id - 1].add(id);
                        arr[id + 8].add(id + 9);
                    }
                }
            }
        }
        ret.add(chessGame.getPlace(chess));
        for (int ii = 0; ii < ret.size(); ii++) {
            Integer i = ret.get(ii);
            //存储顺序上下左右 i - 9, i + 9, i - 1, i + 1
            boolean key[] = {true, true, true, true};
            if(arr[i].size() != 0){
                for (Integer integer : arr[i]) {
                    if(integer > i){
                        if(integer == i + 9){
                            key[1] = false;
                        } else  {
                            key[3] = false;
                        }
                    } else {
                        if(integer == i - 9){
                            key[0] = false;
                        } else  {
                            key[2] = false;
                        }
                    }
                }
            }
            if(i >= 9 && key[0] && !ret.contains(i - 9)){
                ret.add(i - 9);
                if(chess == 0){
                    if(i - 9 < 9){
                        return true;
                    }
                } else {
                    if(i - 9 >= 72){
                        return true;
                    }
                }
            }
            if(i < 72 && key[1] && !ret.contains(i + 9)){
                ret.add(i + 9);
                if(chess == 0){
                    if(i + 9 < 9){
                        return true;
                    }
                } else {
                    if(i + 9 >= 72){
                        return true;
                    }
                }
            }
            if(i % 9 != 0 && key[2] && !ret.contains(i - 1)){
                ret.add(i - 1);
            }
            if((i + 1) % 9 != 0 && key[3] && !ret.contains(i + 1)){
                ret.add(i + 1);
            }
        }
        return false;
    }
}

